package net.htjs.juc.reentrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * sleep和 wait 的区别
 * 1.sleep是 Thread类的静态方法，wait是object的方法
 * 2.sleep使用的时候不强制使用synchronized方法，wait使用必须强制使用synchronized方法
 * 3.sleep睡眠时间不会释放锁，wait等待期间会释放锁
 * 4.他们的状态都是一样的TIMED_WAITING
 * */
@Slf4j
public class ReentrantLockConditionDemo {

    static ReentrantLock room = new ReentrantLock();
    static Condition waitCigrateCondition = room.newCondition();
    static Condition waitTaseoutCondition = room.newCondition();
    static boolean hasCigarette = false;
    static boolean hasTakeout =  false;

    public static void main(String[] args) {
        new Thread(() -> {
            room.lock();
            try{
                log.info("有烟没:[{}]", hasCigarette);
                if(!hasCigarette) {
                    log.info("没烟，先歇会儿");
                    try {
                        waitCigrateCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.info("有烟没:[{}]", hasCigarette);
                if (hasCigarette) {
                    log.info("有烟了，可以开始干活了");
                }
            } finally {
                room.unlock();
            }
        }, "小南").start();

        new Thread(() -> {
            room.lock();
            try{
                Thread thread = Thread.currentThread();
                log.info("外卖送到没?[{}]", hasTakeout);
                if(!hasTakeout) {
                    log.info("没外卖，先歇会");
                    try {
                        waitTaseoutCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.info("外卖送到没?[{}]", hasTakeout);
                if(hasTakeout) {
                    log.info("外卖送到了，可以开始干活了");
                } else {
                    log.info("没干成活");
                }
            } finally {
                room.unlock();
            }
        }, "小女").start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            room.lock();
            try {
                hasCigarette = true;
                log.info("烟到了");
                waitCigrateCondition.signal();
            } finally {
                room.unlock();
            }
        }, "送烟的").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            room.lock();
            try {
                hasCigarette = true;
                log.info("外卖到了");
                waitTaseoutCondition.signal();
            } finally {
                room.unlock();
            }
        }, "送外卖的").start();
    }

}
